<?php
namespace app\gym\controller;
use app\common\controller\AdminBase;
/*小型健身房会员管理系统---  预约列表*/
class Subscribe extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();
    }
	function index(){
		$shop_id=sp_get_current_shop_id();
		$this->assign("ptlist",db("GymEmployee")->where("status=1 and type=3 and shop_id=$shop_id")->select());
		echo $this->fetch();
	}	
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	/*上课*/
	function Up(){
		$pt_follow=db("GymPtFollow")->find(input("id"));
		$member=db("GymMember")->where("id=".$pt_follow['member_id'])->find();
		$pt_order=db("GymPtOrder")->where("id=".$pt_follow['pt_order_id'])->find();
		$this->assign("pt_follow",$pt_follow);
		$this->assign("member",$member);
		$this->assign("pt_order",$pt_order);
		$shop_id=sp_get_current_shop_id();
		$this->assign("ptlist",db("GymEmployee")->field("id,name")->where("status=1 and type=3 and shop_id=$shop_id")->select());
		echo $this->fetch();
	}
	function Up_Post(){
		if(request()->isPost()){
			//判断是否入场
			if(!$this->_is_in(sp_get_current_shop_id(),input("member_id"))){
				$this->ajax_error("您还没有入场");
			}
			$this->_has_overduo(input("pt_order_id"),input("up_time"));
			//修改课程状态
			db("GymPtFollow")->where("id=".input("pt_follow_id"))->setField(
						array(
							"course_status"=>2,
						));
			//修改课程数量			
			db("GymPtOrder")->where("id=".input("pt_order_id"))->setField(
						array(
							"last_course_time"=>input("up_time"),
							"surplus"=>(input("surplus",0)-input("course_count",0)),
						));
			//插入上课记录
			db("GymCourseHistory")->insert(array(
				 "course_name"=>input("course_name"),
				 "course_count"=>input("course_count"),
				 "shop_id"=>input("shop_id"),
				 "pt_order_id"=>input("pt_order_id"),
				 "pt_follow_id"=>input("pt_follow_id"),
				 "up_time"=>input("up_time"),
				 "member_id"=>input("member_id"),
				 "pt_id"=>input("pt_id")
			));					
			$this->ajax_success();
		}	
	}
	//判断是否入场
	function _is_in($shop_id,$member_id){
		$count=db("BehaviorLog")->where("shop_id=$shop_id and member_id=$member_id and TO_DAYS(create_time)=TO_DAYS(now())")->count(1);
		if($count>0){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	//判断上下课是否过期
	function _has_overduo($pt_order_id,$time){
		$order=db("GymPtOrder")->field("start_date,end_date")->where("id=$pt_order_id")->find();
		if(empty($order)){
			$this->ajax_error("无效的私教订单id");
		}else{
			if(($time>$order['start_date']." 00:00:00")&&($time<$order['end_date']." 23:59:59")){
				
			}else{
				return $this->ajax_error("上下课时间不再有效期内","");
			}
		}
	}
	/*下课*/
	function Down(){
		$pt_follow=db("GymPtFollow")->find(input("id"));
		$member=db("GymMember")->where("id=".$pt_follow['member_id'])->find();
		$pt_order=db("GymPtOrder")->where("id=".$pt_follow['pt_order_id'])->find();
		$this->assign("pt_follow",$pt_follow);
		$this->assign("member",$member);
		$this->assign("pt_order",$pt_order);
		$shop_id=sp_get_current_shop_id();
		$this->assign("ptlist",db("GymEmployee")->field("id,name")->where("status=1 and type=3 and shop_id=$shop_id")->select());
		$this->assign("history_id",db("GymCourseHistory")->where("pt_follow_id=".input("id")." and shop_id=".$shop_id)->value("id"));
		echo $this->fetch();
	}
	function Down_Post(){
		if(request()->isPost()){
			if(!$this->_is_in(sp_get_current_shop_id(),input("member_id",0))){
				$this->ajax_error("您还没有入场");
			}
			$this->_has_overduo(input("pt_order_id"),input("down_time"));
			db("GymPtFollow")->where("id=".input("pt_follow_id"))->setField(
						array(
							"course_status"=>1,
			));
			//完成私教课次数
			db("GymMember")->where("id=".input("member_id"))->setInc("over_private_lesson");	
			//插入上课记录
			db("GymCourseHistory")->where("id=".input("history_id"))->update(array(
				 "down_time"=>input("down_time"),
			));					
			$this->ajax_success();
		}	
	}
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10071001");
		$this->assign("menus",$auth_group);
	}
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
		 'member_name'=>array("field" =>"b.name","operator" =>"like"),
		 'member_mobile'=>array("field" =>"b.mobile","operator" =>"like"),
		 'pt_id'=>array("field" =>"c.id","operator" =>"="),
		);
		$shop_id=sp_get_current_shop_id();
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and a.type=2 and a.shop_id=$shop_id and d.id is not null and a.course_status in (0,2)";
		$field="a.id,a.course_status,a.order_time,a.order_date,a.course_count,b.name as member_name,b.mobile as member_mobile,c.name as pt_name,d.course_name";
		$total=db("GymPtFollow")->alias("a")->join([['m_gym_member b',"a.member_id=b.id ","LEFT"],['m_gym_employee c',"c.id=a.pt_id","LEFT"],['m_gym_pt_order d',"d.id=a.pt_order_id","LEFT"]])->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("GymPtFollow")->alias("a")->field($field)->join([['m_gym_member b',"a.member_id=b.id ","LEFT"],['m_gym_employee c',"c.id=a.pt_id","LEFT"],['m_gym_pt_order d',"d.id=a.pt_order_id","LEFT"]])->where($where_and)->order("a.id desc")->limit($this->get_limit())->fetchsql(FALSE)->select();
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}

}
